/*
 * Copyright 2010 David de Mingo
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.shalma.internal.client.view;

import org.shalma.client.Callback;
import org.shalma.client.Persistence;
import org.shalma.client.security.Security;
import org.shalma.client.view.View;
import org.shalma.internal.client.Shell;
import org.shalma.internal.client.ShellIcons;
import org.shalma.internal.reflection.Attribute;
import org.shalma.internal.reflection.MetaClass;
import org.shalma.internal.reflection.MetaClassRegistry;
import org.shalma.model.Entity;
import org.shalma.model.Person;

import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class EntityView extends View {

	interface Binder extends UiBinder<Widget, EntityView> {
	}

	private static Binder binder = GWT.create(Binder.class);

	@UiField(provided = true)
	Image loading;

	@UiField
	protected FlowPanel attributesPanel;

	@UiField
	Style style;

	private Entity entity;

	public EntityView() {

		loading = new Image(ShellIcons.instance.loadingM());

		initWidget(binder.createAndBindUi(this));
	}

	public Object getObject() {
		return entity;
	}

	@Override
	public void onShow() {		
	}
	
	public void update(String token) {

		loading.setVisible(true);
		attributesPanel.clear();

		if (Shell.ME_TOKEN.equals(token)) {
			Security.instance.getUser(new Callback<Person>() {
				@Override
				public void onSuccess(Person result) {
					success(result);
				}
			});
		}

		else {

			Persistence.instance.get(token, new Callback<Entity>() {
				@Override
				public void onSuccess(Entity result) {
					success(result);
				}
			});

			// Query<Entity> query = new Query<Entity>(NamedEntity.NAME,
			// FilterOperator.EQUAL, token);
			// query.setSize(1);
			//
			// Persistence.instance.get(query,
			// new Callback<ArrayList<Entity>>() {
			// @Override
			// public void onSuccess(ArrayList<Entity> result) {
			// Window.alert("TODO");
			// }
			// });

		}
	}

	void success(Entity entity) {

		loading.setVisible(false);

		if (entity == null) {

			Label label = new Label("Entity not found");
			attributesPanel.add(label);

		} else {
			update(entity);
		}
	}

	void update(Entity entity) {

		this.entity = entity;
		MetaClass metaclass = MetaClassRegistry.instance.getMetaClass(entity
				.getClass().getName());

		attributes(metaclass);
	}

	private void attributes(MetaClass metaclass) {

		MetaClass parent = metaclass.getParent();

		if (parent != null)
			attributes(parent);

		for (Attribute attribute : metaclass.getAttributes())
			attributesPanel.add(new AttributeView(attribute, this));
	}

	public void save() {

		Persistence.instance.put(entity, new Callback<Entity>() {
			@Override
			public void onSuccess(Entity entity) {
				update(entity);
			}
		});
	}

	interface Style extends CssResource {

	}

}
